home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 May: Tool Chest / Developer CD Series Tool Chest (Apple Computer)(May 1999).iso / Tool Chest / Games / Game Sample Code / ZAM 1.0a13 / UtilCode / GWorldUtils.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-09-16  |  6.3 KB  |  353 lines  |  [TEXT/KAHL]

  1. /*
  2. //    GWorldUtils.c
  3. //
  4. //    Created:    8/12/91 at 8:01:49 PM
  5. //    By:        Tony Myles
  6. //          Thanks Tony - grabbing this for the CIcon to GWorld stuff
  7. //    Description:    some utility routines to help create graphics worlds
  8. */
  9.  
  10. #include "ZAMProtos.h"
  11.  
  12. #ifndef __MEMORY__
  13. #include <Memory.h>
  14. #endif
  15.  
  16. #ifndef __TOOLUTILS__
  17. #include <ToolUtils.h>
  18. #endif
  19.  
  20. #ifndef __RESOURCES__
  21. #include <Resources.h>
  22. #endif
  23.  
  24. #include "GWorldUtils.h"
  25.  
  26.  
  27.  
  28. /*
  29. //    create a new GWorld optimized for speed in copying
  30. //    to the graphics device that intersects the given rect.
  31. */
  32. OSErr CreateOptimumGWorld(GWorldPtr *optGWorld, Rect *devRect)
  33. {
  34.     OSErr err;
  35.     CGrafPtr saveCPort;
  36.     GDHandle saveGDevice;
  37.     GWorldPtr tempGWorld;
  38.     PixMapHandle pixMapH;
  39.     Rect tempRect = *devRect;
  40.  
  41.     *optGWorld = NULL;
  42.  
  43.     GetGWorld(&saveCPort, &saveGDevice);
  44.  
  45.     LocalToGlobal((Point *)&tempRect.top);
  46.     LocalToGlobal((Point *)&tempRect.bottom);
  47.  
  48.     err = NewGWorld(&tempGWorld, 0, &tempRect, NULL, NULL, 0);
  49.  
  50.     if (err == noErr)
  51.     {
  52.         SetGWorld(tempGWorld, NULL);
  53.         EraseRect(&tempGWorld->portRect);
  54.  
  55.         *optGWorld = tempGWorld;
  56.     }
  57.  
  58.     SetGWorld(saveCPort, saveGDevice);
  59.  
  60.     return err;
  61. }
  62.  
  63. /*
  64.     BRS _ tweaked so I can add a CTable
  65. */
  66. OSErr CreateGWorldWithCTable(GWorldPtr *optGWorld, Rect *devRect, CTabHandle ctable)
  67. {
  68.     OSErr err;
  69.     CGrafPtr saveCPort;
  70.     GDHandle saveGDevice;
  71.     GWorldPtr tempGWorld;
  72.     PixMapHandle pixMapH;
  73.     Rect tempRect = *devRect;
  74.  
  75.     *optGWorld = NULL;
  76.  
  77.     GetGWorld(&saveCPort, &saveGDevice);
  78.  
  79.     LocalToGlobal((Point *)&tempRect.top);
  80.     LocalToGlobal((Point *)&tempRect.bottom);
  81.  
  82.     err = NewGWorld(&tempGWorld, 0, &tempRect, ctable, NULL, 0);
  83.  
  84.     if (err == noErr)
  85.     {
  86.         SetGWorld(tempGWorld, NULL);
  87.         EraseRect(&tempGWorld->portRect);
  88.  
  89.         *optGWorld = tempGWorld;
  90.     }
  91.  
  92.     SetGWorld(saveCPort, saveGDevice);
  93.  
  94.     return err;
  95. }
  96.  
  97. /*
  98. //    creates a offScreen GWorld and draws the specified pict into it
  99. */
  100. OSErr CreateGWorldFromPict(GWorldPtr *pictGWorld, PicHandle pictH)
  101. {
  102.     OSErr err;
  103.     CGrafPtr saveCPort;
  104.     GDHandle saveGDevice;
  105.     GWorldPtr tempGWorld;
  106.     PixMapHandle tempPixHdl;
  107.     Rect pictRect;
  108.  
  109.     *pictGWorld = NULL;
  110.  
  111.     GetGWorld(&saveCPort, &saveGDevice);
  112.  
  113.     pictRect.left = pictRect.top = 0;
  114.     pictRect.right = (**pictH).picFrame.right - (**pictH).picFrame.left;
  115.     pictRect.bottom = (**pictH).picFrame.bottom - (**pictH).picFrame.top;
  116.  
  117.     err = CreateOptimumGWorld(&tempGWorld, &pictRect);
  118.  
  119.     if (err == noErr)
  120.     {
  121.         *pictGWorld = tempGWorld;
  122.  
  123.         SetGWorld(tempGWorld, NULL);
  124.  
  125.         tempPixHdl = GetGWorldPixMap(tempGWorld);
  126.  
  127.         if (LockPixels(tempPixHdl))
  128.         {
  129.             DrawPicture(pictH, &pictRect);
  130.  
  131.             UnlockPixels(tempPixHdl);
  132.         }
  133.     }
  134.  
  135.     SetGWorld(saveCPort, saveGDevice);
  136.  
  137.     return err;
  138. }
  139.  
  140. /*
  141.     BRS - Creates a GWORLD from the pict and attaches the color table to it.
  142. */
  143. OSErr CreateGWorldFromPictWithCTable(GWorldPtr *pictGWorld, PicHandle pictH,CTabHandle ctable)
  144. {
  145.     OSErr err;
  146.     CGrafPtr saveCPort;
  147.     GDHandle saveGDevice;
  148.     GWorldPtr tempGWorld;
  149.     PixMapHandle tempPixHdl;
  150.     Rect pictRect;
  151.  
  152.     *pictGWorld = NULL;
  153.  
  154.     GetGWorld(&saveCPort, &saveGDevice);
  155.  
  156.     pictRect.left = pictRect.top = 0;
  157.     pictRect.right = (**pictH).picFrame.right - (**pictH).picFrame.left;
  158.     pictRect.bottom = (**pictH).picFrame.bottom - (**pictH).picFrame.top;
  159.  
  160.     err = CreateGWorldWithCTable(&tempGWorld, &pictRect, ctable);
  161.  
  162.     if (err == noErr)
  163.     {
  164.         *pictGWorld = tempGWorld;
  165.  
  166.         SetGWorld(tempGWorld, NULL);
  167.  
  168.         tempPixHdl = GetGWorldPixMap(tempGWorld);
  169.  
  170.         if (LockPixels(tempPixHdl))
  171.         {
  172.             DrawPicture(pictH, &pictRect);
  173.  
  174.             UnlockPixels(tempPixHdl);
  175.         }
  176.     }
  177.  
  178.     SetGWorld(saveCPort, saveGDevice);
  179.  
  180.     return err;
  181. }
  182.  
  183.  
  184. OSErr CreateGWorldFromPictResource(GWorldPtr *pictGWorldP, short pictResID)
  185. {
  186.     OSErr err;
  187.     PicHandle newPictH;
  188.  
  189.     newPictH = GetPicture(pictResID);
  190.  
  191.     if (newPictH != NULL)
  192.     {
  193.         err = CreateGWorldFromPict(pictGWorldP, newPictH);
  194.  
  195.         ReleaseResource((Handle)newPictH);
  196.     }
  197.  
  198.     return err;
  199. }
  200.  
  201.  
  202. OSErr CreateGWorldFromCIconResource(GWorldPtr *iconGWorldP, short iconResID)
  203. {
  204.     OSErr err;
  205.     CIconHandle cIconH;
  206.  
  207.     cIconH = GetCIcon(iconResID);
  208.  
  209.     if (cIconH != NULL)
  210.     {
  211.         HNoPurge((Handle)cIconH);
  212.  
  213.         if (iconGWorldP != NULL)
  214.         {
  215.             err = CreateGWorldFromCIcon(iconGWorldP, cIconH);
  216.         }
  217.  
  218.         DisposeCIcon(cIconH);
  219.     }
  220.  
  221.     return err;
  222. }
  223.  
  224.  
  225. OSErr CreateGWorldFromCIcon(GWorldPtr *iconGWorldP, CIconHandle cIconH)
  226. {
  227.     OSErr err;
  228.     CGrafPtr saveCPort;
  229.     GDHandle saveGDevice;
  230.     char saveState;
  231.     GWorldPtr tempGWorldP;
  232.     PixMapHandle iconPixMapH;
  233.     Rect iconRect;
  234.  
  235.     *iconGWorldP = NULL;
  236.  
  237.     GetGWorld(&saveCPort, &saveGDevice);
  238.  
  239.     iconRect = (**cIconH).iconPMap.bounds;
  240.     err = CreateOptimumGWorld(&tempGWorldP, &iconRect);
  241.  
  242.     if (err == noErr)
  243.     {
  244.         *iconGWorldP = tempGWorldP;
  245.         SetGWorld(tempGWorldP, NULL);
  246.  
  247.         iconPixMapH = GetGWorldPixMap(tempGWorldP);
  248.  
  249.         if (LockPixels(iconPixMapH))
  250.         {
  251.             
  252.             PlotCIcon(&iconRect, cIconH);
  253.  
  254.             
  255.             UnlockPixels(iconPixMapH);
  256.         }
  257.  
  258.     }
  259.  
  260.     SetGWorld(saveCPort, saveGDevice);
  261.  
  262.     return err;
  263. }
  264.  
  265.  
  266. OSErr CreateGWorldFromCIconMask(GWorldPtr *maskGWorldP, CIconHandle cIconH)
  267. {
  268.     OSErr err;
  269.     CGrafPtr saveCPort;
  270.     GDHandle saveGDevice;
  271.     char saveState;
  272.     BitMap iconMask;
  273.     GWorldPtr tempGWorldP;
  274.     PixMapHandle maskPixMapH;
  275.  
  276.     GetGWorld(&saveCPort, &saveGDevice);
  277.  
  278.     saveState = HGetState((Handle)cIconH);
  279.     HLock((Handle)cIconH);
  280.  
  281.     iconMask.rowBytes = (**cIconH).iconMask.rowBytes;
  282.     iconMask.bounds = (**cIconH).iconMask.bounds;
  283.     iconMask.baseAddr = (Ptr)(**cIconH).iconMaskData;
  284.  
  285.     err = CreateOptimumGWorld(&tempGWorldP, &iconMask.bounds);
  286.  
  287.     if (err == noErr)
  288.     {
  289.         *maskGWorldP = tempGWorldP;
  290.         SetGWorld(tempGWorldP, NULL);
  291.  
  292.         maskPixMapH = GetGWorldPixMap(tempGWorldP);
  293.  
  294.         if (LockPixels(maskPixMapH))
  295.         {
  296.             CopyBits(&iconMask, (BitMapPtr)*maskPixMapH,
  297.                         &iconMask.bounds, &iconMask.bounds,
  298.                         srcCopy, NULL);
  299.             
  300.             UnlockPixels(maskPixMapH);
  301.         }
  302.     }
  303.  
  304.     SetGWorld(saveCPort, saveGDevice);
  305.     HSetState((Handle)cIconH, saveState);
  306.  
  307.     return err;
  308. }
  309.  
  310.  
  311. OSErr CreateRegionFromCIconMask(RgnHandle *maskRgn, CIconHandle cIconH)
  312. {
  313.     OSErr err = noErr;
  314.     RgnHandle tempMaskRgn;
  315.     char saveState;
  316.     BitMap iconMask;
  317.  
  318.     *maskRgn = NULL;
  319.  
  320.     saveState = HGetState((Handle)cIconH);
  321.     HLock((Handle)cIconH);
  322.  
  323.     iconMask.rowBytes = (**cIconH).iconMask.rowBytes;
  324.     iconMask.bounds = (**cIconH).iconMask.bounds;
  325.     iconMask.baseAddr = (Ptr)(**cIconH).iconMaskData;
  326.  
  327.     tempMaskRgn = NewRgn();
  328.  
  329.     if (tempMaskRgn != NULL)
  330.     {
  331.         err = BitMapToRegion(tempMaskRgn, &iconMask);
  332.  
  333.         if (err == noErr)
  334.         {
  335.             *maskRgn = tempMaskRgn;
  336.         }
  337.         else
  338.         {
  339.             DisposeRgn(tempMaskRgn);
  340.         }
  341.     }
  342.     else
  343.     {
  344.         err = MemError();
  345.     }
  346.  
  347.     HSetState((Handle)cIconH, saveState);
  348.  
  349.     return err;
  350. }
  351.  
  352.  
  353.